/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.mine.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.activiti.engine.impl.util.json.JSONObject;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.mine.entity.Hole;
import com.thinkgem.jeesite.modules.mine.entity.Roadway;
import com.thinkgem.jeesite.modules.mine.service.HoleService;
import com.thinkgem.jeesite.modules.mine.service.MineService;
import com.thinkgem.jeesite.modules.mine.service.RoadwayService;
import com.thinkgem.jeesite.modules.sys.entity.Dict;
import com.thinkgem.jeesite.modules.sys.service.DictService;

/**
 * 采集孔信息Controller
 * @author developer
 * @version 2018-11-05
 */
@Controller
@RequestMapping(value = "${adminPath}/mine/hole")
public class HoleController extends BaseController {

	@Autowired
	private HoleService holeService;
	@Autowired
	private RoadwayService roadwayService;
	@Autowired
	private DictService dictService;
	
	@ModelAttribute
	public Hole get(@RequestParam(required=false) String id) {
		Hole entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = holeService.get(id);
		}
		if (entity == null){
			entity = new Hole();
		}
		return entity;
	}
	
	@RequiresPermissions("mine:hole:view")
	@RequestMapping(value = {"list", ""})
	public String list(Hole hole, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<Hole> page = holeService.findPage(new Page<Hole>(request, response), hole); 
		model.addAttribute("page", page);
		Dict dict = new Dict();
		dict.setType("probe_type");
		model.addAttribute("selectList",dictService.findList(dict));
		return "modules/mine/holeList";
	}

	@RequiresPermissions("mine:hole:view")
	@RequestMapping(value = "form")
	public String form(Hole hole, Model model) {
		model.addAttribute("hole", hole);
		return "modules/mine/holeForm";
	}

	@RequiresPermissions("mine:hole:edit")
	@RequestMapping(value = "save")
	public String save(Hole hole, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, hole)){
			return form(hole, model);
		}
		holeService.save(hole);
		addMessage(redirectAttributes, "保存采集孔信息成功");
		return "redirect:"+Global.getAdminPath()+"/mine/hole/?repage";
	}
	
	@RequiresPermissions("mine:hole:edit")
	@RequestMapping(value = "delete")
	public String delete(Hole hole, RedirectAttributes redirectAttributes) {
		holeService.delete(hole);
		addMessage(redirectAttributes, "删除采集孔信息成功");
		return "redirect:"+Global.getAdminPath()+"/mine/hole/?repage";
	}
	
	@RequiresPermissions("mine:hole:edit")
	@RequestMapping(value = "saveHole")
	@ResponseBody
	public String saveHole(HttpServletRequest request, HttpServletResponse response,Hole hole) throws Exception {
		JSONObject job = new JSONObject();
		if(hole.getAngle() >= 90 || hole.getAngle() <= -90) {
			return job.accumulate("error", "保存失败，采集孔角度必须在-90至90之间，数据无效，请检查！！").toString();

		}
		hole.setRoadname(roadwayService.get(hole.getRoadwayId()).getRoadname());
		holeService.save(hole);
		job.accumulate("success", "采集孔信息保存成功！");
		return job.toString();
	}

}